package com.swak.job.worker;

import java.util.Set;

import com.swak.job.Job;
import com.swak.job.JobConfig;
import com.swak.job.JobService;
import com.swak.job.queue.QueueService;
import com.swak.job.storage.StorageService;
import com.swak.leader.LeaderService;
import com.swak.metrics.MetricsFactory;
import com.swak.registry.RegistryService;
import com.swak.registry.URL;
import com.swak.utils.Sets;
import com.swak.utils.StringUtils;

/**
 * 任务角色
 * 
 * @author lifeng
 * @date 2021年2月25日 上午11:52:44
 */
public interface Worker {

	/**
	 * 地址
	 */
	URL getURL();

	/**
	 * 启动
	 */
	void start();

	/**
	 * 停止
	 */
	void stop();

	/**
	 * 刷新
	 */
	void refresh(JobConfig config);

	/**
	 * 创建Job 服务类
	 * 
	 * @param module
	 * @param job
	 * @param storageService
	 * @param queueService
	 * @param registryService
	 * @return
	 */
	public static Set<Worker> create(Job job, JobService jobService, StorageService storageService,
			QueueService queueService, RegistryService registryService, LeaderService leaderService,
			MetricsFactory metricsFactory) {
		Set<Worker> workers = Sets.newHashSet();
//		if (job.isRetryJob()) {
//			if (StringUtils.isNotBlank(job.getCronExpression())) {
//				workers.add(new RetryWorker(jobService.getTimer(), jobService.getConfig(), job, storageService,
//						queueService, registryService, leaderService, metricsFactory));
//			}
//		} else {
		if (StringUtils.isNotBlank(job.getCronExpression())) {
			workers.add(new DispatchWorker(jobService.getTimer(), jobService.getConfig(), job, storageService,
					queueService, registryService, leaderService, metricsFactory));
		}

		if (job.getExecuteMethod() != null) {
			workers.add(new ExecuteWorker(jobService.getConfig(), job, queueService, registryService, metricsFactory));
		}
		// }
		return workers;
	}
}